package cn.itsy.web.controller;

import cn.itsy.dao.Logindao;
import cn.itsy.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
public class LoginController {
    @Resource
    private Logindao ld;

    @RequestMapping("menu")
    public String menu(String uname, String pass, Model m, HttpServletRequest request) {
        String sql = "select * from user where uname = '" + uname + "' and pass = '" + pass + "' ";
        List<User> loginselect = ld.loginselect(sql);
        if (!loginselect.isEmpty()) {
            HttpSession session = request.getSession();
            session.setAttribute("user", loginselect.get(0));
            String menusql = "select f.* from user u,sys_user_role ur,sys_role r,sys_role_function rf,sys_function f " +
                    "where u.id = ur.user_id and r.id = ur.role_id and r.id = rf.role_id and f.id=rf.function_id and u.uname='" + uname + "'";
            List<Map<String, Object>> menu = ld.menu(menusql);
            m.addAttribute("menu", menu);
            return "index";
        } else {
            m.addAttribute("a", "用户不存在");
            return "login";
        }
    }
}
